package com.feicheng.lanniao.controller.pc;



import java.beans.Transient;
import java.util.List;

import javax.imageio.spi.RegisterableService;
import javax.servlet.http.HttpSession;

import com.feicheng.lanniao.service.QuotaRecordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feicheng.lanniao.entity.Client;
import com.feicheng.lanniao.entity.Person;
import com.feicheng.lanniao.exception.SystemException;
import com.feicheng.lanniao.result.ResultInfo;
import com.feicheng.lanniao.service.IClientService;
import com.feicheng.lanniao.service.IPersonService;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author feichengTech
 * @since 2021-09-02
 */
@RestController
@RequestMapping("/lanniao-user/client-login")
public class LoginController {
	@Autowired
	HttpSession session;
	@Autowired
	IClientService IClientService;
	@Autowired
	IPersonService IPersonService;
	@Autowired
	QuotaRecordService quotaRecordService;
	/**
	 * 注册客户信息
	 * @param account 账户
	 * @param password 密码
	 * @param name 名称
	 * @param tel 电话
	 * @param secId 身份证
	 * @return boolean值
	 */
	@PostMapping("/register")
	@Transient
	public ResultInfo addpeResultInfo(String account, String password, String name, String tel,
			String secId) {	
		Person person = new Person();
		person.setPersonName(name);
		person.setPhone(tel);
		person.setIdentityCard(secId);
		QueryWrapper<Person> perqQueryWrapper = new QueryWrapper<Person>();
		perqQueryWrapper.eq("phone", tel);
		Person person1 = IPersonService.getOne(perqQueryWrapper);
		if(null != person1) {
			throw new SystemException("手机号已存在");
		}
		IPersonService.addPersonInfo(person);
		QueryWrapper<Person> queryWrapper = new QueryWrapper<Person>();
		queryWrapper.eq("phone", person.getPhone());
		Person person2 = IPersonService.getOne(queryWrapper);
		Client client = new Client();
		client.setAccount(account);
		client.setPassword(password);
		client.setDeleteFlag(true);
		client.setPersonId(person2.getPersonId());
		IClientService.addclienInfo(client);
		QueryWrapper<Person> personQueryWrapper = new QueryWrapper<Person>();
		Person person3 = IPersonService.getOne(personQueryWrapper.eq("phone",tel));
		QueryWrapper<Client> clientQueryWrapper = new QueryWrapper<>();
		Client client1 = IClientService.getOne(clientQueryWrapper.eq("person_id",person3.getPersonId()));
		quotaRecordService.initQuota(client1.getClientId());
	return ResultInfo.success(true);
	}

	/**
	 * 客户登录方法
	 * @param client 客户对象
	 * @return boolean值
	 */
	@PostMapping("/login")
	public ResultInfo login(Client client) {
		return IClientService.login(client);		
	}
	/**
	 * 客户退出方法
	 * @param session 会话
	 * @return boolean值
	 */
	@PostMapping("/logout")
	public ResultInfo logout(HttpSession session) {
		session.invalidate();
		System.out.println("我是sessionId:" + session.getId());		
		return ResultInfo.bindingFail("用户退出登录成功！");
	}
//	@PostMapping("/register")
	public ResultInfo register(Client client) {
		return IClientService.register(client);
	}
}